<?php

namespace App\Http\Controllers;

use App\Services\WechatService;
use Exception;
use Illuminate\Http\Request;

class WechatController extends Controller
{
    private $wechatService;

    public function __construct(WechatService $wechatService)
    {
        $this->wechatService = $wechatService;
    }

    /**
     * 支付成功通知
     * @param Request $request
     * @return array|\Symfony\Component\HttpFoundation\Response
     */
    public function notify(Request $request)
    {
        try {
            return $this->wechatService->notify($request);
        } catch (Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }
    }

    /**
     * 获取商户二维码
     * @param Request $request
     * @return array|\EasyWeChat\Kernel\Http\StreamResponse
     */
    public function merchantQRCode(Request $request)
    {
        try {
            $validateRules = [
                'merchantCode' => 'required',
            ];
            $validateMessages = [
                'merchantCode.required' => 'mc不能为空!',
            ];
            $this->requestValidator($request, $validateRules, $validateMessages);
            $merchantCode = $request->input('merchantCode');
            return $this->wechatService->merchantQRCode($merchantCode);
        } catch (Exception $exception) {
            return $this->wrapErrorReturn($exception);
        }
    }
}
